package com.ruoyi.web.controller.base;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.live.base.domain.LiveBarrage;
import com.ruoyi.live.product.service.LiveBarrageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 弹幕管理
 */
@RestController
@RequestMapping({"/base/bulletScreens"})
public class LiveBarrageController extends BaseController {
    @Autowired
    LiveBarrageService service;

    @GetMapping("/list")
    public TableDataInfo query(LiveBarrage entity) {
        this.startPage();
        List<LiveBarrage> list = this.service.query(entity);
        return this.getDataTable(list);
    }

    /**
     * 根据弹幕编号获取详细信息
     */
    @GetMapping(value = "/{barrageId}")
    public AjaxResult getInfo(@PathVariable("barrageId") Long barrageId)
    {
        return AjaxResult.success(service.getLiveBarrage(barrageId));
    }

    @PreAuthorize("@ss.hasAnyRoles('admin')")
    @Log(title = "弹幕管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody LiveBarrage entity) {
        try {
            this.service.addLiveBarrage(entity);
            return AjaxResult.success("新增成功");
        } catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.error(exception.getMessage());
        }
    }

    @PreAuthorize("@ss.hasAnyRoles('admin')")
    @Log(title = "弹幕管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody LiveBarrage entity) {
        try {
            this.service.updateLiveBarrage(entity);
            return AjaxResult.success("修改成功");
        } catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.error(exception.getMessage());
        }
    }

    @PreAuthorize("@ss.hasAnyRoles('admin')")
    @Log(title = "弹幕管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{barrageIds}")
    public AjaxResult remove(@PathVariable Long[] barrageIds) {
        try {
            this.service.deleteLiveBarrage(barrageIds);
            return AjaxResult.success("删除成功");
        } catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.error(exception.getMessage());
        }
    }
}
